package cn.hz.pi.api.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
//	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

//	private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
	
//	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

//	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

//	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
//	private final static SimpleDateFormat Day = new SimpleDateFormat("dd");
	private static int timeSub;

	public static Integer getTimeIntSec(String time) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date da = sf.parse(time);
			return (int) (da.getTime() / 1000 + timeSub);
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return null;
	}
	public static String selectDay() {
		SimpleDateFormat Day = new SimpleDateFormat("dd");
		return Day.format(fomatDate(addDay(getTime(), -1)));
	}
	/**
	 * 获取YYYY格式
	 * @return
	 */
	public static String getYear() {
		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
		return sdfYear.format(new Date());
	}
	/**
	 * 计算两个时间差月数
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	  public static int getMonthSpace(String date1, String date2)
	            throws ParseException {

	        int result = 0;

	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	        Calendar c1 = Calendar.getInstance();
	        Calendar c2 = Calendar.getInstance();

	        c1.setTime(sdf.parse(date1));
	        c2.setTime(sdf.parse(date2));

	        result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);

	        return result == 0 ? 1 : Math.abs(result);

	    }
	/**
	 * 获取两个时间之间的小时
	 * 
	 * @param startDate
	 *            时间1
	 * @param endDate
	 *            时间2
	 * @return 小时数
	 */
	public static long housBetween(String startDate, String endDate) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(startDate));
			long startTime = cal.getTimeInMillis();
			cal.setTime(fomatDate(endDate));
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (1000*60*60*24)));
		} catch (Exception e) {
			return -1;
		}
	}
	/**
	 * 获取YYYY-MM-DD格式
	 * @return
	 */
	public static String getDay() {
		SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
		return sdfDay.format(new Date());
	}

	/**
	 * 获取YYYYMMDD格式
	 * @return
	 */
	public static String getDays() {
		SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
		return sdfDays.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * @return
	 */
	public static String getTime() {
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdfTime.format(new Date());
	}

	/**
	 * @Title: compareDate
	 * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws
	 * @author luguosui
	 */
	public static boolean compareDate(String s, String e) {
		if (fomatDate(s) == null || fomatDate(e) == null) {
			return false;
		}
		return fomatDate(s).getTime() >= fomatDate(e).getTime();
	}
	
	public static boolean compareMonth(String s, String e) {
		s = s + "-01 00:00:00";
		e = e + "-01 00:00:00";
		return compareDate(s,e);
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(date.contains("/")) {
			fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		}
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
		
			return null;
		}
	}

	/**
	 * 格式化日期
	 * @return
	 */
	public static Date fomatDate2(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 校验日期是否合法
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return false;
		}
	}

	public static int getDiffYear(String startTime, String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			long aa = 0;
			int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	/**
	 * <li>功能描述：时间相减得到天数
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
		java.util.Date beginDate = null;
		java.util.Date endDate = null;

		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		// System.out.println("相隔的天数="+day);

		return day;
	}

	/**
	 * 得到n天之后的日期
	 * @param days
	 * @return
	 */
	public static String getAfterDayDate(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdfd.format(date);

		return dateStr;
	}

	/**
	 * 得到n天之后是周几
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);

		return dateStr;
	}

	public static String getFormatTime(Date time) {
		try {
			SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return sdfd.format(time);
		} catch (Exception e) {
		}
		return null;
	}

	public static String getDataTimeFormatLong(long time){
		return getFormatTime(new Date(time));
	}
	/**
	 * 获取两个时间之间的间隔天数
	 * @param startDate 时间1
	 * @param endDate 时间2
	 * @return 天数
	 */
	public static int daysBetween(Date startDate ,Date endDate){
		try{
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			long startTime = cal.getTimeInMillis();
			cal.setTime(endDate);
			long endTime = cal.getTimeInMillis();
			return (int)Math.abs(((endTime-startTime)/(1000*60*60*24))) ;
		}catch(Exception e){
			return -1;
		}
	}
	
	public static String formatDateToHour(String time){
		try{
			Date date = DateUtil.fomatDate(time);
			SimpleDateFormat sfm = new SimpleDateFormat("HH:mm");
			return  sfm.format(date);
		}catch(Exception e){
			return null;
		}
	}
	/**
	 * 获取时间的豪秒数
	 * @param date
	 * @return
	 */
	public static long getDateTimeInMillis(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 获取当天零点日期
	 */
	public  static String getZeroTime(){
		Calendar cal2=Calendar.getInstance();
		cal2.setTime(new Date());
		cal2.set(Calendar.HOUR_OF_DAY, 0);
		cal2.set(Calendar.MINUTE, 0);
		cal2.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal2.getTime()).toString();//当天时间零点;
		
	}
	/**
	 * 获取昨天零点日期
	 */
	public  static String getYesterDayZeroTime(){
		Calendar cal=Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime()).toString();
		
	}
	/**
	 * 获取昨天结束日期(yyyy 23:59:59)
	 */
	public  static String getYesterDayEndTime(){
		Calendar cal=Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DATE, -1);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime()).toString();
		
	}
	/**
	 * 获取当天结束时间
	 * @return
	 * @return Date
	 * @author Wangn
	 * @date 2018年4月18日 下午3:36:09
	 */
	public static String getTodayEndTime() {  
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.setTime(new Date());
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);  
        todayEnd.set(Calendar.MINUTE, 59);  
        todayEnd.set(Calendar.SECOND, 59);  
        return DateUtil.getFormatTime(todayEnd.getTime()).toString();  
    } 
	/**
	 * 获取上周星期一的日期
	 * @param args
	 */
	
	public static String getLastWeekStartDate(){
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DATE, -7);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 * 获取上周星期日的日期
	 * @param args
	 */
	public static String getLastWeekEndDate(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 * 获取本周星期一的日期
	 * @param args
	 */
	
	public static String getPresentWeekStartDate(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 * 获取本周星期日的日期
	 * @param args
	 */
	public static String getPresentWeekEndDate(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
		cal.add(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 *  获取本月开始的日期
	 * @param args
	 */
	public static String getPresentMonthStartDate(){
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 *  获取本月结束的日期
	 * @param args
	 */
	public static String getPresentMonthEndDate(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 *  获取上月开始的日期
	 * @param args
	 */
	public static String getLastMonthStartDate(){
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.MONTH,-1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 *  获取上月结束的日期
	 * @param args
	 */
	public static String getLastMonthEndDate(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 0);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 * 获取本年的开始时间
	 * @param args
	 */
	public static String getCurrentYearStart(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.MONTH, 0);
		cal.set(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return DateUtil.getFormatTime(cal.getTime());
	}
	/**
	 * 获取本年的结束时间
	 * @param args
	 */
	public static String getCurrentYearEnd(){
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.MONTH, 11);
		cal.set(Calendar.DATE, 31);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return DateUtil.getFormatTime(cal.getTime());
	}
	
	

	public static String addMonth(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MONTH, num);
		return sdfTime.format(calendar.getTime());
	}

	public static String addDay(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.DATE, num);
		return sdfTime.format(calendar.getTime());
	}
	

	public static String addMinute(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MINUTE, num);
		return sdfTime.format(calendar.getTime());
	}
	public static String addHour(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.HOUR, num);
		return sdfTime.format(calendar.getTime());
	}
	
	/**
	 * 减num个小时
	 * @param time
	 * @param num
	 * @return
	 */
	public static String reduceHour(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
			calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - num);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sdfTime.format(calendar.getTime());
	}
	
	public static String getTime(String time){
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sdfTime.format(calendar.getTime());
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getLastTime(new Date()));
	}
	public static String getMonth() {
		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
		return sdfMonth.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * 
	 * @return
	 */
	public static String getDay(String time) {
		SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
		return sdfDay.format(fomatDate(time));
	}
	public static String getMonth(String time) {
		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");
		return sdfMonth.format(fomatDate(time));
	}

	public static String getTimeByDate(Date time) {
		try {
			SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return null == time ? null : sdfTime.format(time);
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * <li>功能描述：时间相减得到小时数
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getHourSub(String beginDateStr, String endDateStr) {
		try {
			Date beginDate = fomatDate(beginDateStr);
			Date endDate = fomatDate(endDateStr);

			return Math.abs((endDate.getTime() - beginDate.getTime()) / (60 * 1000));
		} catch (Exception e) {
		}
		return 0;
	}
	
	/**
	 * 把时间推多少分钟
	 * @param args
	 */
	public static String addTimeInMillis(double millis){
		long mi=(long) (millis*60*1000);
		return DateUtil.getDataTimeFormatLong((DateUtil.getDateTimeInMillis(new Date())-mi));
	}


	public static String addSecond(String time, int num) {
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.SECOND, num);
		return sdfTime.format(calendar.getTime());
	}
	
	/**
	 * 时间转换：秒数转成时分秒
	 */
	public static String  timeFormatChange(String time){
		if(null!=time){
			double sum=Double.valueOf(time);
			int hour=(int)Math.floor(sum/(1*60*60));
			int minit=(int)Math.floor((sum-hour*1*60*60)/(1*60));
			long second=Math.round((sum-hour*1*60*60-minit*1*60));

			String reStr = "";
			if (hour > 0) {
				reStr += hour + "时";
			} else {

			}
			if (minit > 0) {
				reStr += minit + "分";
			} else {

			}
			if (second > 0) {
				reStr += second + "秒";
			} else {

			}
			
			
			return reStr;
		}else{
			return "";
		}
	}

	/**
	 * 把秒数转换为"XX分XX秒"字符串形式
	 * 
	 * @param second
	 * @return:String
	 * @author wn
	 * @date 2018年1月31日 下午3:45:14
	 */
	public static String getMinAndSec(Long second) {
		long min = 0;
		long sec = 0;
		if (second != null && second > 0) {
			min = second / 60;
			sec = second % 60;
		}

		return min + " 分 " + sec + " 秒";
	}
	
	/**
	 * 校验日期是否合法
	 * 
	 * @return
	 */
	public static boolean isValidTime(String s) {
		try {
			SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sdfTime.parse(s);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 获取两个时间之间的间隔秒数
	 * 
	 * @param startDate
	 *            时间1
	 * @param endDate
	 *            时间2
	 * @return 天数
	 */
	public static long secondBetween(String startDate, String endDate) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(startDate));
			long startTime = cal.getTimeInMillis();
			cal.setTime(fomatDate(endDate));
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (1000)));
		} catch (Exception e) {
			return -1;
		}
	}
	
	/**
	 * 获取两个时间之间的间隔秒数
	 * 
	 * @param startDate
	 *            时间1
	 * @param endDate
	 *            时间2
	 * @return 天数
	 */
	public static long minBetween(String startDate, String endDate) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(startDate));
			long startTime = cal.getTimeInMillis();
			cal.setTime(fomatDate(endDate));
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (60*1000)));
		} catch (Exception e) {
			return -1;
		}
	}
	
	public static long minDataBetween(String startDate, String endDate,int i) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(startDate));
			long startTime = cal.getTimeInMillis();
			cal.setTime(fomatDate(endDate));
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (60*1000*i)));
		} catch (Exception e) {
			return -1;
		}
	}
	
	
	public static long minDateBetween(Date startDate, Date endDate) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(startDate);
			long startTime = cal.getTimeInMillis();
			cal.setTime(endDate);
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (60*1000)));
		} catch (Exception e) {
			return -1;
		}
	}
	

	/**
	 * 
	 * @param month
	 *            月份（格式：yyyy-MM）
	 * @param num
	 * @return 月份（格式：yyyy-MM）
	 */
	public static String monthAdd(String month, int num) {

		SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");

		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Calendar calendar = Calendar.getInstance();

		try {
			String time = month + "-01 00:00:00";
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MONTH, num);
		return sdfMonth.format(calendar.getTime());
	}

	/**
	 * 获取本月最后一天
	 * 
	 * @return 格式（yyyy-MM-dd hh:mm:ss）
	 */
	public static String getMonthEndDate() {

		try {
			Calendar cal = Calendar.getInstance();

			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));

			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			return DateUtil.getFormatTime(cal.getTime());
		} catch (Exception e) {
		}

		return "";
	}
	
	
	/**
	 * 获取本月最后一天
	 * 
	 * @return 格式（yyyy-MM-dd hh:mm:ss）
	 */
	public static String getMonthEndDate(Integer year) {

		try {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.YEAR, year);	
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			return DateUtil.getFormatTime(cal.getTime());
		} catch (Exception e) {
		}

		return "";
	}
	
	/**
	 * 获取*年*月的开始日期
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getStartByYearAndMonth(int year,int month){
		YearMonth yearMonth=YearMonth.of(year, month);
	     LocalDate localDate=yearMonth.atDay(1);
	     LocalDateTime startOfDay=localDate.atStartOfDay();
	     ZonedDateTime zonedDateTime=startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
	     Date date=Date.from(zonedDateTime.toInstant());
		return DateUtil.getFormatTime(date);
	}
	/**
	 * 获取*年*月的结束日期
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getEndByYearAndMonth(int year,int month){
		 YearMonth yearMonth=YearMonth.of(year, month);
	     LocalDate endofmonth=yearMonth.atEndOfMonth();
	     LocalDateTime localDateTime=endofmonth.atTime(23,59,59,999);
	     ZonedDateTime zonedDateTime=localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
	     Date date=Date.from(zonedDateTime.toInstant());
	     return DateUtil.getFormatTime(date);
	}
	
	/**
	 * 获取*年*月一共有多少天
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDayNumber(int year,int month){
		Calendar cal=Calendar.getInstance();
		cal.set(year, month, 0);
		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取指定月份最后一天
	 * 
	 * @param month
	 *            指定月份(yyyy-MM)
	 * @return
	 */
	public static String getMonthEndDate(String month) {
		Calendar cal = Calendar.getInstance();
		try {

			SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM");

			cal.setTime(sdfTime.parse(month));
			cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return getDay(getFormatTime(cal.getTime()));
	}
	/**
	 * 根据年月查询此月的所有日期
	 * @param year
	 * @param month
	 * @return
	 */
	public static String[] getAllDateByMonth(int year,String month){
		int num=DateUtil.getDayNumber(year, Integer.valueOf(month));
		String[] arr=new String[num];
		for(int i=0;i<num;i++){
			if(i<9){
				arr[i]=year+"-"+month+"-0"+(i+1);
			}else{
				arr[i]=year+"-"+month+"-"+(i+1);
			}
		}
		return arr;
	}

	/**
	 * 
	 * @param year
	 *            年份（格式：yyyy）
	 * @param num
	 * @return 年份（格式：yyyy）
	 */
	public static String addYear(String year, int num) {

		SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Calendar calendar = Calendar.getInstance();

		try {
			String time = year + "-01-01 00:00:00";
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.YEAR, num);
		return sdfYear.format(calendar.getTime());
	}
	
	/**
	 * 在指定年(yyyy-MM)的基础上加或者减指定年个数    后期修改
	 * 
	 * @Time 2017年8月28日下午4:03:36
	 * @param
	 * @param month
	 * @param num
	 * @return
	 */
	public static String yearAdd(String time, int num) {

		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Calendar calendar = Calendar.getInstance();

		try {
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.YEAR, num);
		return sdfTime.format(calendar.getTime());
	}
	
	
	/**
	 * 获取该月第一天
	 * 
	 * @Time 2017年8月28日下午2:08:19
	 * @param
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最小天数
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH, firstDay);
		// 格式化日期
		String firstDayOfMonth = getFormatTime(cal.getTime());
		return firstDayOfMonth;
	}
	
	
	/**
	 * 获取该月最后一天
	 * 
	 * @Time 2017年8月28日下午2:08:50
	 * @param
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		String lastDayOfMonth = getFormatTime(cal.getTime());
		return lastDayOfMonth;
	}
	/**
	 * 2017-10
	 * 工具月获取该月多少天
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getDayNumberS(String months){
		int year =Integer.valueOf(months.substring(0,4));
		int month=Integer.valueOf(months.substring(5,7));
		return getDayNumber(year, month);
	}

	/**
	 * 
	 * @param m1
	 *            月份（格式:yyyy-MM）
	 * @param m2
	 *            月份（格式:yyyy-MM）
	 * @return
	 */
	public static int monthBetween(String m1, String m2) {
		int count = 0;
		try {
			String time1 = m1.concat("-01 00:00:00");
			if (!isValidTime(time1))
				return 0;
			String time2 = m2.concat("-01 00:00:00");
			if (!isValidTime(time2))
				return 0;

			while (DateUtil.compareDate(time2, time1)) {
				if (DateUtil.compareDate(addMonth(time1, count), time2))
					break;
				count++;
			}

			while (DateUtil.compareDate(time1, time2)) {
				if (DateUtil.compareDate(addMonth(time2, count), time1))
					break;
				count++;
			}
		} catch (Exception e) {
		}

		return count;
	}

	public static String dayAdd(String day, int num) {

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		Calendar calendar = Calendar.getInstance();

		try {
			String time = day + " 00:00:00";
			SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			calendar.setTime(sdfTime.parse(time));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.DATE, num);
		return format.format(calendar.getTime());
	}

	public static boolean compareDay(String s, String e) {
		s = s + " 00:00:00";
		e = e + " 00:00:00";
		return compareDate(s, e);
	}

	/**
	 * 获取指定时点所对应日期的零点
	 * 
	 * @param time
	 * @return
	 */
	public static String getZeroTime(String time) {
		try {
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(fomatDate(time));
			cal2.set(Calendar.HOUR_OF_DAY, 0);
			cal2.set(Calendar.MINUTE, 0);
			cal2.set(Calendar.SECOND, 0);
			return DateUtil.getFormatTime(cal2.getTime()).toString();// 当天时间零点;
		} catch (Exception e) {
		}
		return null;
	}

	public static String getDayEndTime(String time) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(time));
			cal.set(Calendar.HOUR_OF_DAY, 23);
			cal.set(Calendar.MINUTE, 59);
			cal.set(Calendar.SECOND, 59);
			return DateUtil.getFormatTime(cal.getTime()).toString();
		} catch (Exception e) {
		}
		return null;
	}
	
	/**
	 * 获取两个日期之间的所有日期
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public static String[] getBetweenDates(String startTime, String endTime) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start = sdf.parse(startTime);
		Date end = sdf.parse(endTime);
		
	    List<String> result = new ArrayList<String>();
	    Calendar tempStart = Calendar.getInstance();
	    tempStart.setTime(start);
	    
	    Calendar tempEnd = Calendar.getInstance();
	    tempEnd.setTime(end);
	    tempEnd.add(Calendar.DAY_OF_YEAR, 1);
	    
	    while (tempStart.before(tempEnd)) {
	        result.add(sdf.format(tempStart.getTime()));
	        tempStart.add(Calendar.DAY_OF_YEAR, 1);
	    }
	    
	    String[] str = (String[])result.toArray(new String[result.size()]);
	    return str;
	}
	
	/**
	 * 获取两个时间之间的所有月份
	 * @param minDate
	 * @param maxDate
	 * @return
	 * @throws ParseException
	 */
	public static String[] getMonthBetween(String minDate, String maxDate) throws ParseException {
	    ArrayList<String> result = new ArrayList<String>();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

	    Calendar min = Calendar.getInstance();
	    Calendar max = Calendar.getInstance();

	    min.setTime(sdf.parse(minDate));
	    min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

	    max.setTime(sdf.parse(maxDate));
	    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

	    Calendar curr = min;
	    while (curr.before(max)) {
	     result.add(sdf.format(curr.getTime()));
	     curr.add(Calendar.MONTH, 1);
	    }

	    return (String[])result.toArray(new String[result.size()]);
	  }
		
	/**
	 * 获取两个时间之间的天
	 * 
	 * @param startDate
	 *            时间1
	 * @param endDate
	 *            时间2
	 * @return 天数
	 */
	public static long dayBetween(String startDate, String endDate) {
		try {
			Calendar cal = Calendar.getInstance();
			cal.setTime(fomatDate(startDate));
			long startTime = cal.getTimeInMillis();
			cal.setTime(fomatDate(endDate));
			long endTime = cal.getTimeInMillis();
			return Math.abs(((endTime - startTime) / (1000*60*60*24)));
		} catch (Exception e) {
			return -1;
		}
	}
	/**
	 * 获取当前值的前一个
	 * @param date
	 * @return
	 */
	public static String getLastTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int min = cal.get(cal.MINUTE);
		if(min >=0 && min<15) {
			cal.set(cal.MINUTE, 0-15);
		}
		if(min >=15 && min<30) {
			cal.set(cal.MINUTE, 15-15);
		}
		if(min >=30 && min<45) {
			cal.set(cal.MINUTE, 30-15);
		}
		if(min >=45 && min<60) {
			cal.set(cal.MINUTE, 45-15);
		}
		date = cal.getTime();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		return sdfTime.format(date);
	}
	
	/**
	 * 获取当前值的前一个
	 * @param date
	 * @return
	 */
	public static String getLastTime_15(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int min = cal.get(cal.MINUTE);
		if(min >=0 && min<15) {
			cal.set(cal.MINUTE, 0-15);
		}
		if(min >=15 && min<30) {
			cal.set(cal.MINUTE, 15-15);
		}
		if(min >=30 && min<45) {
			cal.set(cal.MINUTE, 30-15);
		}
		if(min >=45 && min<60) {
			cal.set(cal.MINUTE, 45-15);
		}
		date = cal.getTime();
		cal.setTime(date);
		
		cal.add(cal.MINUTE, -15);
		date = cal.getTime();
		SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		return sdfTime.format(date);
	}
	
	
	

}
